//
//  HelloWorldLayer.m
//  zhuan
//
//  Created by zhao ys on 5/27/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"
#import "playLayer.h"
#import "settingLayer.h"
#import "SoundManager.h"
#import "GameCenter.h"
#import "modeSelectLayer.h"

// HelloWorldLayer implementation
@implementation modeSelectLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	modeSelectLayer *layer = [modeSelectLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(CCMenuItem *) menuItemWithMode:(NSString*)mode target:(id)target  selector:(SEL)selector {
	CCSprite * sprite = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_jiesuan_bg.png"]];
	NSString * str = [NSString stringWithFormat:@"%@", mode];

	CCLabelTTF * label = [CCLabelTTF labelWithString:str fontName:@"Marker Felt" fontSize:64];
	label.color = ccc3(64, 0, 0);
	CGSize sSize = sprite.contentSize;
	label.position = ccp(sSize.width / 2, sSize.height / 2);
	[sprite addChild:label];
	
	CCMenuItem * item = [CCMenuItemSprite  itemFromNormalSprite:sprite selectedSprite:nil target:target selector:selector];
	//item.tag = i;
	item.scale = 0.5;

	return item;
}

-(CCMenuItem *) menuWithPng:(NSString*)png target:(id)target selector:(SEL)selector {
//	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
//	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
	CCSprite * button_normal = [CCSprite spriteWithFile:png];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:nil target:target selector:selector];
	return button;
}


// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		CGSize size = [[CCDirector sharedDirector] winSize];
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];

		//load back ground
		CCSpriteFrame * bgframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_startmenu_bg.png"];
		CCSprite * bg = [CCSprite spriteWithSpriteFrame:bgframe];
		bg.position = ccp(size.width/2, size.height/2);
		[self addChild:bg z:-1];
	
	
		//trinket
		CCSprite * trinket = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_002.png"]];
		trinket.position = ccp(350, 130);
		[self addChild:trinket];
		
		
		{
			s_levelup_ = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			CGSize tSize = s_levelup_.contentSize;
			[self addChild:s_levelup_ z:0 tag:8];
			s_levelup_.position = ccp(size.width / 4 - 60, size.height / 2);
			CCSprite * trinkets1_a = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets1_a.position = ccp(tSize.width/2, tSize.height*3/2);
			[s_levelup_ addChild:trinkets1_a];
			CCSprite * trinkets2_b = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets2_b.position = ccp(tSize.width/2, -tSize.height/2);
			[s_levelup_ addChild:trinkets2_b];
			CCMenuItem * levelup = [self menuWithPng:@"dkz_normal.png" target:self selector:@selector(OnLevelUp:)];
			levelup.position = ccp(tSize.width/2, size.height/2 - 20);
			CCMenuItem * menu_g = [CCMenu menuWithItems:levelup,nil];
			menu_g.position = ccp(0,0);
			[s_levelup_ addChild:menu_g];
		}
		{		
			s_endless_ = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			CGSize tSize = s_endless_.contentSize;
			[self addChild:s_endless_ z:0 tag:8];
			s_endless_.position = ccp(size.width * 2 / 4, size.height / 2);
			CCSprite * trinkets1_a = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets1_a.position = ccp(tSize.width/2, tSize.height*3/2);
			[s_endless_ addChild:trinkets1_a];
			CCSprite * trinkets2_b = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets2_b.position = ccp(tSize.width/2, -tSize.height/2);
			[s_endless_ addChild:trinkets2_b];
			CCMenuItem * levelup = [self menuWithPng:@"dkz_endless.png" target:self selector:@selector(OnEndless:)];
			levelup.position = ccp(tSize.width/2, size.height/2 - 20);
			CCMenuItem * menu_g = [CCMenu menuWithItems:levelup,nil];
			menu_g.position = ccp(0,0);
			[s_endless_ addChild:menu_g];
		}
		
		{			
			s_hard_= [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			CGSize tSize = s_hard_.contentSize;
			[self addChild:s_hard_ z:0 tag:8];
			s_hard_.position = ccp(size.width * 3 / 4 + 60, size.height / 2);
			CCSprite * trinkets1_a = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets1_a.position = ccp(tSize.width/2, tSize.height*3/2);
			[s_hard_ addChild:trinkets1_a];
			CCSprite * trinkets2_b = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets2_b.position = ccp(tSize.width/2, -tSize.height/2);
			[s_hard_ addChild:trinkets2_b];
			CCMenuItem * levelup = [self menuWithPng:@"dkz_challenge.png" target:self selector:@selector(OnHard:)];
			levelup.position = ccp(tSize.width/2, size.height/2 - 20);
			CCMenuItem * menu_g = [CCMenu menuWithItems:levelup,nil];
			menu_g.position = ccp(0,0);
			[s_hard_ addChild:menu_g];
		}
		
		{
			CCSprite * sprite_back = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_back.png"]];
			CCMenuItem * button_back = [CCMenuItemSprite itemFromNormalSprite:sprite_back selectedSprite:nil target:self selector:@selector(OnMenuBack:)];
			button_back.position = ccp(size.width - 30, size.height - 30);
			CCMenu * menu_back = [CCMenu menuWithItems:button_back, nil];
			menu_back.position = ccp(0,0);
			[self addChild:menu_back];
		}
		

/*		
		NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
		NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
		
		if (dict == nil) {
			return nil;
		}
		
		NSArray * mode_array = [[dict objectForKey:@"classic"] objectForKey:@"level"];

		int offset = size.width / (mode_array.count + 2);
		CCMenu * menu = [CCMenu menuWithItems:nil];
		for (int i = 0; i < mode_array.count; i++) {
			CCMenuItem * item = [self menuItemWithMode:i];
			item.position = ccp(offset * (i+1), size.height / 2);
			[menu addChild:item];
		}
		
		CCMenuItem * item = [self menuItemWithMode:-1];
		item.position = ccp(offset * (mode_array.count+1), size.height / 2);
		[menu addChild:item];
		menu.position = ccp(0,0);
		[self addChild:menu];
*/		
		
//		int offset = size.width / (mode_array.count + 2);
/*
		CCMenuItem * item0 = [self menuItemWithMode:@"levelup" target:self selector:@selector(OnLevelUp:)];	
		CCMenuItem * item1 = [self menuItemWithMode:@"endless" target:self selector:@selector(OnEndless:)];
		CCMenuItem * item2 = [self menuItemWithMode:@"hard" target:self selector:@selector(OnHard:)];
		CCMenu * menu = [CCMenu menuWithItems:item0, item1, item2, nil];
		[menu alignItemsHorizontallyWithPadding:20];
		
		[self addChild:menu];
*/		
	}
	return self;
}

-(void) onEnter {
	NSLog(@"HelloWorldLayer OnEnter");
	[super onEnter];
	
	CGSize size = [[CCDirector sharedDirector] winSize];
	
	s_levelup_.position = ccp(s_levelup_.position.x, 100); 
	[s_levelup_ runAction:[CCEaseElasticOut actionWithAction:
						   [CCMoveTo actionWithDuration:5 position:ccp(s_levelup_.position.x, size.height/2)] period:0.5f]];
	
	
	s_hard_.position = ccp(s_hard_.position.x, 100);
	[s_hard_ runAction:  [CCEaseElasticOut actionWithAction:
							 [CCMoveTo actionWithDuration:8 position:ccp(s_hard_.position.x, size.height/2)] period:0.5f]];
	


	s_endless_.position = ccp(s_endless_.position.x, 100);
	[s_endless_ runAction:  [CCEaseElasticOut actionWithAction:
							 [CCMoveTo actionWithDuration:10 position:ccp(s_endless_.position.x, size.height/2)] period:0.5f]];
}

-(void) OnLevelUp :(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"mode_select" loops:1];
	[(CCNode*)sender runAction:[CCScaleTo actionWithDuration:0.3 scale:0.9]];

	[GameLogic ScoreMode];
	[GameLogic ResetGameConfig];
//	int row = [[leve_config objectForKey:@"row"] intValue];
//	int col = [[leve_config objectForKey:@"col"] intValue];
//	int type_cnt = [[leve_config objectForKey:@"types"] intValue];
//	int count_per_type = [[leve_config objectForKey:@"count_per_type"] intValue];
//	time_max_ = [[leve_config objectForKey:@"time_limit"] intValue];
	[[GameLogic sharedGameLogic] gen]; //:row col:col type_cnt:type_cnt count_per_type:count_per_type];
	
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}

-(void) OnEndless :(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"mode_select" loops:1];
	[(CCNode*)sender runAction:[CCScaleTo actionWithDuration:0.3 scale:0.9]];

	[GameLogic EndlessMode];
	[GameLogic ResetGameConfig];
	[[GameLogic sharedGameLogic] gen];
	
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}

-(void) OnHard :(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"mode_select" loops:1];
	[(CCNode*)sender runAction:[CCScaleTo actionWithDuration:0.3 scale:0.9]];

	
	[GameLogic ClassicMode];
	[GameLogic ResetGameConfig];
	time_max_ = 150; //[[leve_config objectForKey:@"time_limit"] intValue];
	[[GameLogic sharedGameLogic] gen];
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}


-(void) OnMenuBack:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"click" loops:1];

	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[HelloWorldLayer scene]]];
}


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
